package com.escape.subject.domain.handler.subject;

import com.escape.subject.common.enums.IsDeletedEnum;
import com.escape.subject.common.enums.SubjectInfoTypeEnum;
import com.escape.subject.domain.convert.MultipleSubjectConverter;
import com.escape.subject.domain.entity.SubjectAnswerBO;
import com.escape.subject.domain.entity.SubjectInfoBO;
import com.escape.subject.domain.entity.SubjectOptionBO;
import com.escape.subject.infra.basic.entity.SubjectMultiple;
import com.escape.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-26  11:54
 * @Description: 多选题目策略类
 * @Version: 1.0
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    /**
     * 添加多选题答案
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 1. 判空
        if (subjectInfoBO.getOptionList() == null) {
            return;
        }

        // 2. 遍历选项
        List<SubjectMultiple> subjectMultipleList = subjectInfoBO.getOptionList().stream().map(option -> {
            SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            return subjectMultiple;
        }).collect(Collectors.toList());

        // 3. 插入多选答案
        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    /**
     * 查询多选题答案
     * @param subjectInfoBO
     * @return
     */
    @Override
    public SubjectOptionBO query(SubjectInfoBO subjectInfoBO) {
        // 1. 查询多选题答案
        SubjectMultiple multiple = new SubjectMultiple();
        multiple.setSubjectId(subjectInfoBO.getId());
        multiple.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.batchQueryByCondition(multiple);

        // 2. 转换
        List<SubjectAnswerBO> subjectAnswerBOList = MultipleSubjectConverter.INSTANCE.convertListToBoList(subjectMultipleList);

        // 3. 封装 & 返回
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
